package com.zyw.crawler.services;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.zip.ZipOutputStream;

import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zyw.controller.CrawlerController;
import com.zyw.util.CompactAlgorithm;
import com.zyw.util.DownloadFile;
import com.zyw.util.HttpRequestParser;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class CrawlerForGpgs implements PageProcessor {
	public static final String START_PAGE = "http://www.neeq.com.cn/nq/listedcompany.html";
	public static final String PARENT_LIST_PAGE = "http://www.neeq.com.cn/nqxxController/nqxx.do";
	public static final String INFO_LIST_PAGE = "http://www.neeq.com.cn/disclosureInfoController/infoResult.do";
	public static final String typejb = "T";// T-上市公司 B-两网及退市公司
	public static final String HOST = "http://www.neeq.com.cn/";
	static Boolean openInfoListManyPage = true;
	static Boolean openParentListManyPage = true;
	static String FILE_CACHE_PATH;
	static String PutInOrder_PATH;
	static final Integer threadNum = 50;
	static String All_Excel_PATH;
	public static String dabao_Path;
	public static String dabao_File;
	static {
		// 加载文件缓存目录
		FILE_CACHE_PATH = CrawlerForGpgs.class.getResource("/").getPath() + "/cacheFile/";
		PutInOrder_PATH = CrawlerForGpgs.class.getResource("/").getPath() + "/result/putInOrder/";
		All_Excel_PATH  = CrawlerForGpgs.class.getResource("/").getPath() + "/result/汇总.xlsx";
		dabao_Path =CrawlerForGpgs.class.getResource("/").getPath() + "/result/";
		
	}
	private static void dabao() {
		dabao_File ="result_"+System.currentTimeMillis();
		CompactAlgorithm ca=new CompactAlgorithm();
		ZipOutputStream zos=null;
		try {
			zos=new ZipOutputStream(new FileOutputStream(CrawlerForGpgs.class.getResource("/").getPath()+dabao_File+".zip"));
			ca.zipDirectory(new File(dabao_Path),zos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if(zos!=null)zos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {

	}

	public CrawlerForGpgs() {
		
	}

	private static void clearParamary() {
		runObj = null;// 将查询条件写入静态变量
		companyList = new ArrayList<CompanyInfo>();// 初始化公司信息list
		parent_maxTotalPage = -1;// 初始化总页面最大页数
		parent_MaxInfoNum = -1;// 初始化总页面信息总记录数
		try {
			File f=new File(FILE_CACHE_PATH);
			for(File children:f.listFiles()){
				children.delete();//清除缓存文件
			}
			clearResult(new File(dabao_Path));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	  /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * @param dir 将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful.
     *                 If a deletion fails, the method stops attempting to
     *                 delete and returns "false".
     */
	 private static boolean clearResult(File dir) {
	        if (dir.isDirectory()) {
	            String[] children = dir.list();
	            //递归删除目录中的子目录下
	            for (int i=0; i<children.length; i++) {
	                boolean success = clearResult(new File(dir, children[i]));
	                if (!success) {
	                    return false;
	                }
	            }
	        }
	        // 目录此时为空，可以删除
	        return dir.delete();
	  }

	static Spider spider;

	public static void stop() {
		if (spider != null) {
			spider.stop();
		}
	}

	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

	public static long startTime;
	public static long endTime;
	/**
	 * 从前台传输过来的任务执行请求
	 */
	public static void startTask(RequestObj obj) {
		clearParamary();// 清理参数
		runObj = obj;// 将查询条件写入静态变量
		companyList = new ArrayList<CompanyInfo>();// 初始化公司信息list
		parent_maxTotalPage = -1;// 初始化总页面最大页数
		parent_MaxInfoNum = -1;// 初始化总页面信息总记录数
		startTime=System.currentTimeMillis();
		endTime=-1;
		CrawlerController.state = "running";
		long startCrawler=System.currentTimeMillis();
		spider = Spider.create(new CrawlerForGpgs()).addUrl(obj.generateStartPage(PARENT_LIST_PAGE)).thread(threadNum);// 创建爬虫任务
		spider.run();// 执行爬虫任务
		
		if(CrawlerController.state.equals("stop"))return;//当手工停止任务时,不执行后续操作。
		System.out.println("抓取文件耗时:"+(System.currentTimeMillis()-startCrawler)+"ms");
		CrawlerController.state = "analysis";// 设定整理任务状态
		long putInOrderCrawler=System.currentTimeMillis();
		putInOrderFile();//整理文件
		System.out.println("整理文件耗时:"+(System.currentTimeMillis()-putInOrderCrawler)+"ms");
		long cr=System.currentTimeMillis();
		collectResult();//汇总结果
		System.out.println("汇总结果耗时:"+(System.currentTimeMillis()-cr)+"ms");
		CrawlerController.state = "dabao";// 设定任务状态-打包
		dabao();
		CrawlerController.state = "end";// 设定结束任务状态
		clearParamary();// 清理参数
		endTime=System.currentTimeMillis();
		System.out.println("总共耗时:"+(endTime-startTime)+"ms");
	}
	

	private static void collectResult(){
		//创建保存结果的xlsx文件
		XSSFWorkbook xssf=null;
		try {
			xssf=new XSSFWorkbook();
			XSSFSheet sheet=xssf.createSheet();//创建空白页
			XSSFRow titleRow=sheet.createRow(0);//创建标题行
			titleRow.createCell(0).setCellValue("公司代码");
			titleRow.createCell(1).setCellValue("公司简称");
			titleRow.createCell(2).setCellValue("所属行业");
			titleRow.createCell(3).setCellValue("地区");
			titleRow.createCell(4).setCellValue("主办券商");
			titleRow.createCell(5).setCellValue("转让类型");
			titleRow.createCell(6).setCellValue("创新层/基础层");
			titleRow.createCell(7).setCellValue("整理文件目录(后续内容以此目录下文件为准)");
			titleRow.createCell(8).setCellValue("收集文件数量");
			titleRow.createCell(9).setCellValue("公开转让说明书");
			titleRow.createCell(10).setCellValue("最新年度报告");
			titleRow.createCell(11).setCellValue("最新半年度报告");
			//创建特殊格式生成器
			CreationHelper creationHelper=xssf.getCreationHelper();
			for(int i=0;i<companyList.size();i++){
				CompanyInfo company=companyList.get(i);
				XSSFRow row=sheet.createRow(i+1);//创建内容行
				row.createCell(0).setCellValue(company.getCompanyCode());
				row.createCell(1).setCellValue(company.jiancheng);
				row.createCell(2).setCellValue(company.hangye);
				row.createCell(3).setCellValue(company.diqu);
				row.createCell(4).setCellValue(company.zhubanquanshang);
				row.createCell(5).setCellValue(company.zhuanrangleixing);
				row.createCell(6).setCellValue(company.getType());
				//
				XSSFHyperlink link=(XSSFHyperlink) creationHelper.createHyperlink(Hyperlink.LINK_FILE);
				link.setAddress("/"+company.getCompanyCode()+"/");
				row.createCell(7).setHyperlink(link);
				//
				row.createCell(8).setCellValue(company.fileInfo.size());
				for(CompanyFileInfo fileInfo:company.fileInfo){
					try {
						if(fileInfo.title.contains("公开转让说明书")){
							XSSFHyperlink l=(XSSFHyperlink) creationHelper.createHyperlink(Hyperlink.LINK_FILE);
							l.setAddress("/"+company.getCompanyCode()+"_"+company.jiancheng+"/"+fileInfo.title+".pdf");
							row.createCell(9).setHyperlink(l);
						}
						if(fileInfo.title.contains("年年度报告")){
							XSSFHyperlink l=(XSSFHyperlink) creationHelper.createHyperlink(Hyperlink.LINK_FILE);
							l.setAddress("/"+company.getCompanyCode()+"_"+company.jiancheng+"/"+fileInfo.title+".pdf");
							row.createCell(10).setHyperlink(l);
						}
						if(fileInfo.title.contains("年半年度报告")){
							XSSFHyperlink l=(XSSFHyperlink) creationHelper.createHyperlink(Hyperlink.LINK_FILE);
							l.setAddress("/"+company.getCompanyCode()+"_"+company.jiancheng+"/"+fileInfo.title+".pdf");
							row.createCell(11).setHyperlink(l);
						}
					} catch (Exception e) {
						//e.printStackTrace();
					}
				}
				//添加整理的邮箱,电话
			}
			if(new File(All_Excel_PATH).exists())
			{
				new File(All_Excel_PATH).delete();
			}
				new File(All_Excel_PATH).createNewFile();
			FileOutputStream outputStream = new FileOutputStream(All_Excel_PATH);
			xssf.write(outputStream);
			outputStream.flush();
			xssf.close();//关闭文件
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				xssf.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	public static void putInOrderFile() {
		// 如果整理文件根文件夹不存在创建根文件夹
		if (!new File(PutInOrder_PATH).exists() || !new File(PutInOrder_PATH).isDirectory())
			new File(PutInOrder_PATH).mkdirs();
		// 循环公司信息
		if (companyList == null)
			companyList = new ArrayList<CompanyInfo>();
		for (CompanyInfo companyInfo : companyList) {
			// 按公司code创建当前公司的文件夹
			new File(PutInOrder_PATH + "/" + companyInfo.getCompanyCode() +"_" +companyInfo.jiancheng+ "/").mkdir();
			// 复制所有当前公司文件到此文件目录下all文件夹

			//第一步,将所有属于当前公司的文件复制到当前公司文件下all目录,并按到request fileType进行分类保存
			new File(PutInOrder_PATH + "/" + companyInfo.getCompanyCode() +"_" +companyInfo.jiancheng+ "/all/").mkdir();
			
			for (int i=0;i<companyInfo.fileInfo.size();i++) {
				CompanyFileInfo fileInfo= companyInfo.fileInfo.get(i);
				String cacheFilePath = fileInfo.path + fileInfo.cacheName;
				String regex = ".*[?|*|/|\\|<|>|\"|'].*";
				String newFileName = "";
				if (!Pattern.matches(regex, fileInfo.title)) {
					newFileName = fileInfo.title + "." + fileInfo.type;
				} else {
					newFileName = fileInfo.cacheName;
				}
				//复制文件到all
				try {
					Copy(cacheFilePath,PutInOrder_PATH + "/" + companyInfo.getCompanyCode() + "_"+companyInfo.jiancheng +  "/all/"+newFileName);
				} catch (Exception e) {
					e.printStackTrace();
				}
				//将对应文件复制到相应目录下
				for(String mbtype:runObj.getFileType()){
					if(fileInfo.title.contains(mbtype)){
						String path=PutInOrder_PATH + "/" + companyInfo.getCompanyCode() + "_"+companyInfo.jiancheng +  "/"+mbtype+"/";
						if(!new File(path).exists())
						{
							new File(path).mkdir();
						}
						Copy(cacheFilePath, path+"/"+newFileName);
					}
				}
			}
			//第二步,在公司目录下创建一个excel,记录所有的文件信息
			XSSFWorkbook xssf=null;
			try {
				xssf=new XSSFWorkbook();//创建xlsx 工作空间
				XSSFSheet sheet=xssf.createSheet();//创建一个空白页
				XSSFRow titleRow=sheet.createRow(0);//创建标题行
				titleRow.createCell(0).setCellValue("编号");
				titleRow.createCell(1).setCellValue("文件名称");
				titleRow.createCell(2).setCellValue("股转系统中的上传日期");
				titleRow.createCell(3).setCellValue("文件原始地址(股转系统文件地址)");
				titleRow.createCell(4).setCellValue("文件类型");
				titleRow.createCell(5).setCellValue("内容页数(针对pdf文件)");
				for (int i=0;i<companyInfo.fileInfo.size();i++) {//循环读取属于当前公司的文件信息
					XSSFRow row=sheet.createRow(i+1);//创建行
					//以下为文件名处理
					CompanyFileInfo fileInfo= companyInfo.fileInfo.get(i);
					String cacheFilePath = fileInfo.path + fileInfo.cacheName;
					String regex = ".*[?|*|/|\\|<|>|\"|'].*";
					String newFileName = "";
					if (!Pattern.matches(regex, fileInfo.title)) {
						newFileName = fileInfo.title + "." + fileInfo.type;
					} else {
						newFileName = fileInfo.cacheName;
					}
					row.createCell(0).setCellValue(i);//编号
					row.createCell(1).setCellValue(newFileName);//文件名
					row.createCell(2).setCellValue(fileInfo.date);
					row.createCell(3).setCellValue(fileInfo.fileUrl);
					//文件类型
					for(String mbtype:runObj.getFileType()){
						if(fileInfo.title.contains(mbtype)){
							row.createCell(4).setCellValue(mbtype);
						}
					}
					if(fileInfo.type.equals("pdf")){
						//内容页数
						row.createCell(5).setCellValue(PdfReader.getPDfPageSize(cacheFilePath));
					}
				}
				//输出文件
				FileOutputStream outputStream = new FileOutputStream(PutInOrder_PATH + "/" + companyInfo.getCompanyCode() + "/"+companyInfo.jiancheng+"_文件信息列表.xlsx");
				xssf.write(outputStream);
				outputStream.flush();
				xssf.close();//关闭文件
			} catch (Exception e) {
				// TODO: handle exception
			}
			//第三步,从文件内容中,获取相关信息
			//包含:编号,关键字,行内容,所属文件,第几页
			//获取检索文件path
			String sousuojieguo=PutInOrder_PATH + "/" + companyInfo.getCompanyCode()+"_"+companyInfo.jiancheng+ "/"+companyInfo.jiancheng+"_关键字检索结果.xlsx";
			List<String> gjz=null;
			if(runObj!=null&&runObj.gjz!=null){
				gjz=runObj.gjz;
			}else{
				gjz=Arrays.asList(PdfReader.defGuanJianZi);
			}
  			//获取信息,并保存到公司目录下的内容检索.xlsx
  			PdfReader.findInfoFromPdfs(companyInfo.fileInfo,gjz,sousuojieguo);
  			//统计pdf内词频
  			PdfReader.InfoWordCountFromPdfs(companyInfo.fileInfo,PutInOrder_PATH + "/" + companyInfo.getCompanyCode()+"_"+companyInfo.jiancheng+ "/"+companyInfo.jiancheng+"_内容词频统计.xlsx");
  			//pdf文本汇总
  			PdfReader.saveAllInfoInText(companyInfo.fileInfo,PutInOrder_PATH + "/" + companyInfo.getCompanyCode()+"_"+companyInfo.jiancheng + "/"+companyInfo.jiancheng+"_pdf文本内容汇总.txt");
		}
	}

	public static void Copy(String oldPath, String newPath) { 
		try {
			int byteread = 0;
			File oldfile = new File(oldPath);
			if(!new File(newPath).exists())new File(newPath).createNewFile();
			if (oldfile.exists()) {
				InputStream inStream = new FileInputStream(oldPath);
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Site getSite() {
		return site;
	}

	static RequestObj runObj;
	static Integer parent_maxTotalPage = -1;
	static Integer parent_MaxInfoNum = -1;
	static List<CompanyInfo> companyList;

	public void process(Page page) {
		try {
			// 如果是总列表,执行当前解析
			if (page.getUrl().regex(PARENT_LIST_PAGE).match()) {
				if (openParentListManyPage) {
					findMaxPageNumberForParentListPage(page);// 增加父列表页面
					addInfoListTaskPage(page);// 增加 infoList page
				}
				findCompanyInfoInPage(page);// 获取公司信息
			}
			// 如果是公司信息列表,用这个解析
			if (page.getUrl().regex(INFO_LIST_PAGE).match()){
				
				if (openInfoListManyPage)
					findMaxPageNumberForInfoListPage(page);// 增加info list 列表页面
				findCompanyInfoInInfoPage(page);// 获取公司信息
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取父列表页面最大页数
	 * 
	 * @param page
	 * @return
	 */
	public void findMaxPageNumberForParentListPage(Page page) {
		try {
			// 获取总页面列表
			JSONArray jsonArray = JSONObject
					.parseArray(page.getJson().toString().substring(1, page.getJson().toString().length() - 1));
			JSONObject json = JSONObject.parseObject(jsonArray.get(0).toString());
			for (int i = 0; i < json.getIntValue("totalPages"); i++) {
				String url = page.getUrl().get();
				url = url.replaceAll("page=" + HttpRequestParser.parse(url).getParameter("page"), "page=" + i);
				page.addTargetRequest(url);
			}
		} catch (Exception e) {
			CrawlerController.state = "Error:findMaxPageNumberForParentListPage";
			e.printStackTrace();
		}
	}

	/**
	 * 添加内容页面列表读取
	 * 
	 * @param page
	 */
	private void addInfoListTaskPage(Page page) {
		try {
			// 获取总页面列表
			JSONArray jsonArray = JSONObject
					.parseArray(page.getJson().toString().substring(1, page.getJson().toString().length() - 1));
			JSONObject json = JSONObject.parseObject(jsonArray.get(0).toString());
			JSONArray contents = JSONObject.parseArray(json.getString("content"));
			for (Object obj : contents) {
				JSONObject content = JSONObject.parseObject(obj.toString());
				String companyCode = content.getString("xxzqdm");
				String infoListStartPage = InfoListReqDefObj.generateReqUrl(companyCode);
				page.addTargetRequest(infoListStartPage);// 新增列表页面
			}
		} catch (Exception e) {
			CrawlerController.state = "Error:addInfoListTaskPage";
			e.printStackTrace();
		}
	}

	/**
	 * 从信息列表首页面获取最大页数,然后想任务列表中,加入新的抓取任务。
	 * 
	 * @param page
	 */
	private void findMaxPageNumberForInfoListPage(Page page) {
		try {
			// 获取总页面列表
			JSONArray jsonArray = JSONObject
					.parseArray(page.getJson().toString().substring(1, page.getJson().toString().length() - 1));
			JSONObject json = JSONObject.parseObject(jsonArray.get(0).toString());
			JSONObject listInfoJsonObj = JSONObject.parseObject(json.get("listInfo").toString());
			for (int i = 0; i < listInfoJsonObj.getIntValue("totalPages"); i++) {
				page.addTargetRequest(InfoListReqDefObj.generateReqUrl(i,
						HttpRequestParser.parse(page.getUrl().get()).getParameter("companyCd")));
			}
		} catch (Exception e) {
			CrawlerController.state = "crawlerJsonError->findMaxPageNumberForInfoListPage";
			e.printStackTrace();
		}
	}

	// 从父列表中获取信息
	private void findCompanyInfoInPage(Page page) {
		try {
			// 获取总页面列表
			JSONArray jsonArray = JSONObject
					.parseArray(page.getJson().toString().substring(1, page.getJson().toString().length() - 1));
			JSONObject json = JSONObject.parseObject(jsonArray.get(0).toString());
			JSONArray contents = JSONObject.parseArray(json.getString("content"));
			for (Object obj : contents) {
				JSONObject content = JSONObject.parseObject(obj.toString());
				String companyCode = content.getString("xxzqdm");// 公司代码
				String infoListStartPage = InfoListReqDefObj.generateReqUrl(companyCode);// 内容列表起始页面
				String hangye = content.getString("xxhyzl");// 公司行业
				String jiancheng = content.getString("xxzqjc");// 公司简称
				String zhubanquanshang = content.getString("xxzbqs");// 主办券商
				String diqu = content.getString("xxssdq");// 地区
				String zhuanrangleixing = content.getString("xxzrlx");// 转让类型
				CompanyInfo info = new CompanyInfo();
				info.companyCode = companyCode;// 公司代码
				info.hangye = hangye;// 行业
				info.jiancheng = jiancheng;// 简称
				info.infoListStartPage = infoListStartPage;// 开始页面
				info.zhubanquanshang = zhubanquanshang;
				info.diqu = diqu;// 地区
				info.zhuanrangleixing = zhuanrangleixing;// 转让类型
				info.setType(content.getIntValue("xxfcbj")==1?"创新层":content.getIntValue("xxfcbj")==0?"基础层":"");
				addCompanyInfo(info);
			}
		} catch (Exception e) {
			CrawlerController.state = "Error:addInfoListTaskPage";
			e.printStackTrace();
		}
	}

	/**
	 * 记录companyInfo 存在重复记录处理原则:老信息不存在的数据,由新信息补充
	 * 
	 * @param info
	 */
	private void addCompanyInfo(CompanyInfo info) {
		for (int i = 0; i < companyList.size(); i++) {
			CompanyInfo c = companyList.get(i);
			if (c.getCompanyCode().equals(info.getCompanyCode())) {
				// 校验判断c对象空值 info对象中是否存在,存在则附加到c对象中
				for (Field f : c.getClass().getFields()) {
					try {
						if (f.get(c) == null && f.get(info) != null) {
							f.set(c, f.get(info));// 将 info的值赋予 c
						}
					} catch (Exception e) {
						System.err.println("Error:向list添加数据时,转换对象失败。");
						CrawlerController.state = "Error:向list添加数据时,转换对象失败。";
					}
				}
				// 存在重复值时,当两值合并后,直接返回
				return;
			}
		}
		// 可以添加
		companyList.add(info);
	}

	private void findCompanyInfoInInfoPage(Page page) {
		try {
			JSONArray jsonArray = JSONObject
					.parseArray(page.getJson().toString().substring(1, page.getJson().toString().length() - 1));
			JSONObject json = JSONObject.parseObject(jsonArray.get(0).toString());
			JSONObject listInfoJsonObj = JSONObject.parseObject(json.get("listInfo").toString());
			JSONArray contentList = JSONObject.parseArray(listInfoJsonObj.get("content").toString());
			for (Object content : contentList) {
				JSONObject obj = JSONObject.parseObject(content.toString());
				// 检测文件是否是需要类型
				Boolean need = false;
				if (runObj != null && runObj.fileType != null) {
					for (String t : runObj.fileType) {
						if (obj.getString("disclosureTitle").contains(t)) {
							need = true;
							break;
						}
					}
				}
				if (!need) {
					continue;
				}
				//TODO 以上已测试,没问题
				String companyCode = obj.getString("companyCd");// 公司代码
				CompanyInfo cacheInfo = null;
				Integer listIndex = -1;
				for (int i = 0; i < companyList.size(); i++) {
					CompanyInfo company = companyList.get(i);
					if (company.getCompanyCode().equals(companyCode)) {
						cacheInfo = company;
						listIndex = i;
						break;
					}
				}
				CompanyFileInfo fileInfo = new CompanyFileInfo();
				fileInfo.fileUrl = HOST + obj.getString("destFilePath");
				fileInfo.title = obj.getString("disclosureTitle").replaceAll("[【】、.。,，。\"!--;:?\'\r\n]","_");
				fileInfo.date = obj.getString("publishDate");
				fileInfo.type = obj.getString("fileExt");
				if (cacheInfo == null) {
					cacheInfo = new CompanyInfo();
					cacheInfo.companyCode = companyCode;// 公司代码
					cacheInfo.jiancheng = obj.getString("companyName");// 公司简称
					// 下载文件
					String path = FILE_CACHE_PATH;
					String cacheName = UUID.randomUUID().toString() + "." + fileInfo.type;
					DownloadFile.download(fileInfo.fileUrl, cacheName, path);
					fileInfo.path = path;
					fileInfo.cacheName = cacheName;
					// 保存
					if (cacheInfo.fileInfo == null) {
						cacheInfo.setFileInfo(new ArrayList<CompanyFileInfo>());
						cacheInfo.getFileInfo().add(fileInfo);
					}
					companyList.add(cacheInfo);
				} else {
					
					
					// 下载文件
					String path = FILE_CACHE_PATH;
					String cacheName = UUID.randomUUID().toString() + "." + fileInfo.type;
					DownloadFile.download(fileInfo.fileUrl, cacheName, path);
					fileInfo.path = path;
					fileInfo.cacheName = cacheName;
					// 保存
					if (companyList.get(listIndex).getFileInfo() == null) {
						companyList.get(listIndex).setFileInfo(new ArrayList<CompanyFileInfo>());
						companyList.get(listIndex).getFileInfo().add(fileInfo);
					}else{
						companyList.get(listIndex).getFileInfo().add(fileInfo);
					}
				}
			}
		} catch (Exception e) {
			CrawlerController.state = "crawlerJsonError->findCompanyInfoInInfoPage";
			e.printStackTrace();
		}
	}

}
